package me.zhengjie.app.system.rest;

import com.alibaba.fastjson.JSONObject;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.app.system.service.AppUserService;
import me.zhengjie.app.system.service.dto.LoginUser;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/appApi/user")
public class AppUserController {

    @Resource
    AppUserService appUserService;

    /**
     * 移动端登录：手机号+密码登录
     *
     * @param aappUser
     * @param request
     * @return
     */
    @AnonymousAccess
    @PostMapping(value = "/phoneLogin")
    public ResponseEntity<Object> login(@Validated @RequestBody LoginUser aappUser, HttpServletRequest request) {
        return ResponseEntity.ok(appUserService.login(aappUser, request));
    }

    /**
     * 移动端退出：
     *
     * @param aappUser
     * @param request
     * @return
     */
    @AnonymousAccess
    @PostMapping(value = "/phoneLogout")
    public ResponseEntity<Object> logout(@Validated @RequestBody LoginUser aappUser, HttpServletRequest request) {
        return ResponseEntity.ok(appUserService.logout(aappUser, request));
    }

    @AnonymousAccess
    @PostMapping(value = "/smsCode")
    public ResponseEntity<Object> smsCode(@Validated @RequestBody String phoneParams) {
        JSONObject mapJson=JSONObject.parseObject(phoneParams);
        return ResponseEntity.ok(appUserService.smsCode(mapJson.get("mobile").toString()));
    }

    @AnonymousAccess
    @PostMapping(value = "/mobileLogin")
    public ResponseEntity<Object> mobileLogin(@Validated @RequestBody LoginUser aappUser,HttpServletRequest request) {
        return ResponseEntity.ok(appUserService.mobileLogin(aappUser,request));
    }

    @AnonymousAccess
    @GetMapping(value = "/userInfo")
    public ResponseEntity<Object> userInfo(HttpServletRequest request) {
        return ResponseEntity.ok(appUserService.getUserInfo(request));
    }

}
